package com.ayf.payment.game.api.controller.platform;

import cn.hutool.core.collection.CollUtil;
import com.ayf.payment.game.api.dto.platform.SmsChannelConfigDTO;
import com.ayf.payment.game.api.dto.platform.SmsContentDTO;
import com.ayf.payment.game.api.entity.platform.SmsChannelConfig;
import com.ayf.payment.game.api.entity.platform.SmsContent;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.platform.SmsChannelConfigService;
import com.ayf.payment.game.api.service.platform.SmsContentService;
import com.ayf.payment.game.api.service.platform.SmsRecordService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.type.SMSType;
import com.ayf.payment.game.api.utils.CommonResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 短信业务处理
 */
@RequestMapping("/t/sms")
@RestController
public class AdminSmsController {

    @Autowired
    private SmsChannelConfigService smsChannelConfigService;
    @Autowired
    private SmsContentService smsContentService;
    @Autowired
    private SmsRecordService smsRecordService;

    /**
     * 短信通道列表查询
     *
     * @return
     */
    @PostMapping("/smsChannelList")
    public CommonResult<?> smsChannelList() {
        List<SmsChannelConfig> list = smsChannelConfigService.getSmsChannelConfigList();
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "短信通道列表查询成功",list);
    }

    /**
     * 更新状态
     * @param smsChannelConfigDTO
     * @return
     */
    @PostMapping("/updateSmsChannelStatus")
    public CommonResult<?> updateSmsChannelStatus(@RequestBody SmsChannelConfigDTO smsChannelConfigDTO) {
        smsChannelConfigService.updateSmsChannelStatus(smsChannelConfigDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "状态更新成功",null);
    }

    /**
     * 更新短信渠道配置
     * @param smsChannelConfigDTO
     * @return
     */
    @PostMapping("/updateSmsChannel")
    public CommonResult<?> updateSmsChannel(@RequestBody SmsChannelConfigDTO smsChannelConfigDTO) {
        smsChannelConfigService.updateSmsChannel(smsChannelConfigDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "更新成功",null);
    }


    /**
     * 短信内容列表查询
     *
     * @return
     */
    @PostMapping("/smsContentList")
    public CommonResult<?> smsContentList() {
        List<SmsContent> list = smsContentService.getSmsContentList();
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "短信内容列表查询成功",list);
    }

    @PostMapping("/saveSmsContent")
    public CommonResult<?> saveSmsContent(@RequestBody SmsContentDTO smsContentDTO) {
        if (StringUtils.isEmpty(smsContentDTO.getSmsChannelCode())){
            throw new ParameterException("短信通道编码不能为空");
        }
        if (StringUtils.isEmpty(smsContentDTO.getSmsChannelName())){
            throw new ParameterException("短信通道名称不能为空");
        }
        if (StringUtils.isEmpty(smsContentDTO.getSmsType())){
            throw new ParameterException("短信内容类型不能为空");
        }
        if (StringUtils.isEmpty(smsContentDTO.getSmsContent())){
            throw new ParameterException("短信内容不能为空");
        }
        smsContentService.saveSmsContent(smsContentDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "短信内容保存成功",null);
    }

    @PostMapping("/deleteSmsContent")
    public CommonResult<?> deleteSmsContent(@RequestBody SmsContentDTO smsContentDTO) {
        if (smsContentDTO.getId()== null){
            throw new ParameterException("删除失败，短信内容ID不能为空");
        }
        smsContentService.deleteSmsContent(smsContentDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "短信内容删除成功",null);
    }

    @PostMapping("/deleteSmsByIds")
    public CommonResult<?> deleteSmsByIds(@RequestBody SmsContentDTO smsContentDTO) {
        if (CollUtil.isEmpty(smsContentDTO.getIds())){
            throw new ParameterException("删除失败，至少选择一条短信");
        }
        smsRecordService.deleteSmsByIds(smsContentDTO);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "短信内容删除成功",null);
    }

    @PostMapping("/smsTypeList")
    public CommonResult<?> smsTypeList() {
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "短信类型列表查询成功", SMSType.getSmsTypeList());
    }

}
